package com.mipo.company.flytek.cycore.api.zhangye;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.thrift.TException;

import com.iflytek.cycore.usercenter.ClientFactory;
import com.iflytek.cycore.usercenter.api.TClass;
import com.iflytek.cycore.usercenter.api.TEduorg;
import com.iflytek.cycore.usercenter.api.TObject;
import com.iflytek.cycore.usercenter.api.TRole;
import com.iflytek.cycore.usercenter.api.TSchool;
import com.iflytek.cycore.usercenter.api.TUser;
import com.iflytek.cycore.usercenter.api.TUserDetail;
import com.iflytek.cycore.usercenter.api.TUserManager;
import com.iflytek.cycore.usercenter.api.TUserWrapper;
import com.iflytek.cycore.usercenter.define.KEYConstants;

public class Main {
	public static void main(String[] args) throws Exception {
		//reset_password_by_unique_info(); // 修改密码
		//retrieveTeacherExt(); // 查询教师用户
		
		//2190000241000000026杨科
		//teacherTransferSchool("2190000241000000040", "2300000001000691720", "3162000039000012578");//zylaoshi01  2190000241000000040 3162000039000012578  张掖市实验中学  2300000001000691720 张掖中学
		//setUserToClass("2190000241000000040", "teacher", "3193234241000000002");
		//老师换学校班级完成end
		
		// 教研员换机构部门
		//String oldEduOrgId = "3162000039000000047";
		//String newEduOrgId = "3162000039000000047";
		//removeUserManageEduorg("2190000241000000024", oldEduOrgId);  // 移除机构管理员
		//setUserToEduorg("2190000241000000024", newEduOrgId); // 设置机构
		
		
		// 学生换班
		//removeUserFromClass("2190000241000000009", "student", "3193234241000000002");
		//setUserToClass("2190000241000000009", "student", "3193234241000000001");

		// 家长 只能修改自己的信息
		//updateUser();
		
		
		
		//getUser();
		
		
		updateUser("2190000241000000004", "622224200408280516", "17771426731", "coolwind@163.com", "高伟");
	}
	
	public static void updateUser(String userId, String id_number, String phone, String email, String name) throws Exception {
		Map<String, TObject> userInfo = new HashMap<String, TObject>();
		userInfo.put("id", TObject.strVal(userId));					//被修改人userId
		if (StringUtils.isNotBlank(name)) 
			userInfo.put("user_name", TObject.strVal(name));		//被修改人姓名
		if (StringUtils.isNotBlank(id_number)) 
			userInfo.put("id_card_no", TObject.strVal(id_number));	//被修改人身份证
		if (!"null".equals(phone) && StringUtils.isNotBlank(phone)) 
			userInfo.put("mobile", TObject.strVal(phone));			//被修改人手机号
		if (!"null".equals(email) && StringUtils.isNotBlank(email)) 
			userInfo.put("email", TObject.strVal(email));			//被修改人邮箱
		userInfo.put("modificator", TObject.strVal(userId));		//修改人userId
		CyClientFactory cyClientFactory = new CyClientFactory();
		String result = cyClientFactory.createUserSvc().update_user(userInfo);
		System.out.println(result);
	}
	
	
	public static void getUser() throws Exception {
		CyClientFactory cyClientFactory = new CyClientFactory();
		/*user:TUser(id:2190000241000000023, 
    	 * delFlag:0, 
    	 * loginName:woshils1, 
    	 * userPassword:0703ff3f8f974ba943aa2b35179569eaec25261b0f7c91f6301d1960, 
    	 * userName:我是校管, 
    	 * gender:0, 
    	 * idCardNo:421122197905302578, 
    	 * appName:rrt_zhangye, 
    	 * creator:2190000241000000002, 
    	 * createTime:2018-03-14 18:20:43, 
    	 * updateTime:2018-05-16 11:03:43, 
    	 * reserved1:0, 
    	 * forbidden:0, 
    	 * userStatus:0 */
		TUserWrapper tuser = cyClientFactory.createUserSvc().get_user_by_login_name("gszytest01");
		String usreId = tuser.user.id;
		String name = tuser.user.userName;
		String mobile = tuser.user.mobile;
		String email = tuser.user.email;
		String idCardNo = tuser.user.idCardNo;
		
		Map<String, TObject> map = new HashMap<String, TObject>();
		map.put("USER_LOGIN_NAME", TObject.strVal("gszytest01"));
		List<TUser> f =cyClientFactory.createUserSvc().retrieve_user_in_school(map);
		f = cyClientFactory.createUserSvc().retrieve_user_info(map);
		TUserDetail t =cyClientFactory.createUserSvc().get_user_detail("2190000241000000004");
		
		List<TSchool> schools = cyClientFactory.createSchoolSvc().list_school_by_user(0, 10, "2190000241000000004");
		List<TClass> classes = cyClientFactory.createClassSvc().list_class_by_user(0, 10, "2190000241000000004");
		List<TEduorg> eduorgs = cyClientFactory.createEduorgSvc().list_eduorg_by_user(0, 10, "2190000241000000004");
		List<TRole> roles = cyClientFactory.createRoleSvc().list_role_by_user("2190000241000000004");
		
		
		
		
		System.out.println(tuser);
	}
	
	public static void updateUser() throws Exception {
		CyClientFactory cyClientFactory = new CyClientFactory();
		Map<String, TObject> userInfo = new HashMap<String, TObject>();
		userInfo.put("id", TObject.strVal("2190000241000000019"));			//被修改人userId
		
		userInfo.put("user_name", TObject.strVal("gszyjz2"));				//被修改人姓名
		userInfo.put("id_card_no", TObject.strVal("420115198522015786"));	//被修改人身份证
		userInfo.put("modificator", TObject.strVal("420115198522015786"));	//修改人userId
		
//		userMap.put(KEYConstants.USER_ID, TObject.strVal(user.getId()));
//		userMap.put(KEYConstants.USER_USER_NAME, TObject.strVal(userName));
//		userMap.put(KEYConstants.USER_ID_CARD_NO, TObject.strVal(idCard));
//		userMap.put(KEYConstants.USER_GENDER, TObject.strVal(gender));
//		userMap.put(KEYConstants.USER_EMAIL, TObject.strVal(email));
//		userMap.put(KEYConstants.USER_MODIFICATOR, TObject.strVal(user.getId()));
		
		String result = cyClientFactory.createUserSvc().update_user(userInfo);
		
		System.out.println(result);
	}
	
	
	public static boolean removeUserFromClass(String userId, String role, String classId)throws Exception{
		List<String> userIds = new ArrayList<String>();
		userIds.add(userId);
		CyClientFactory cyClientFactory = new CyClientFactory();
		String result = cyClientFactory.createUserSvc().remove_user_from_class(userIds, role, classId);
		return Boolean.valueOf("1".equals(result));
	}
	
	
	
	public static boolean setUserToEduorg(String userId, String eduorgId)throws Exception{
		List<String> userIds = new ArrayList<String>();
		userIds.add(userId);
		CyClientFactory cyClientFactory = new CyClientFactory();
		String result = cyClientFactory.createUserSvc().set_user_to_eduorg(userIds, "edupersonnel", eduorgId);
		return Boolean.valueOf("1".equals(result));
	}
	
	
	//移除机构管理员
	public static boolean removeUserManageEduorg(String userId, String eduorgId) throws Exception {
		List<String> userIds = new ArrayList<String>();
		userIds.add(userId);
		CyClientFactory cyClientFactory = new CyClientFactory();
		String result = cyClientFactory.createUserSvc().remove_user_from_eduorg(userIds, "edupersonnel", eduorgId);
		result = cyClientFactory.createUserSvc().remove_user_from_eduorg(userIds, "ledccityMng", eduorgId);
		//String result = cyClientFactory.createUserSvc().remove_user_manage_eduorg(userId, eduorgIds);
		return Boolean.valueOf("1".equals(result));
	}
	
	public static boolean setUserToClass(String userId, String role, String classId)throws Exception{
		CyClientFactory cyClientFactory = new CyClientFactory();
		List<String> userIds = new ArrayList<String>();
		userIds.add(userId);
		String result = cyClientFactory.createUserSvc().set_user_to_class(userIds, role, classId);
		return Boolean.valueOf("1".equals(result));
	}
	
	public static boolean teacherTransferSchool(String userId, String newSchoolId, String oldSchoolId) throws Exception {
		List<UserManager> userManagers = listUserManagerByUserId(userId);
		System.out.println(userManagers.size());
		if ((userManagers != null) && (userManagers.size() > 0)) {
			List<String> schoolIds = new ArrayList();
	        List<String> classIds = new ArrayList();
	        for (UserManager userManager : userManagers) {
				if ("01".equals(userManager.getType())) {
					schoolIds.add(userManager.getLocationId());
				} else if ("04".equals(userManager.getType())) {
					classIds.add(userManager.getLocationId());
				}
			}
	        if (schoolIds.size() > 0) {
	        	removeUserManageSchool(userId, schoolIds);
	        }
	        if (classIds.size() > 0) {
	        	removeUserManageClass(userId, classIds);
	        }
		}
		List<String> userIds = new ArrayList();
        userIds.add(userId);
        List<TRole> roleList = listRoleByUser(userId);
        if ((roleList != null) && (roleList.size() > 0)) {
          for (TRole roleType : roleList) {
            if ("ledcschoolMng".equals(roleType.getEnName())) {
              removeUserFromSchoolToHistory(userIds, "ledcschoolMng", oldSchoolId, true);
            } else if ("teacher".equals(roleType.getEnName())) {
            	System.out.println("teacher");
              removeUserFromSchoolToHistory(userIds, "teacher", oldSchoolId, true);
            }
          }
        }
        setUserToSchool(userIds, "teacher", newSchoolId);
		return true;
	}
	
	public static Boolean setUserToSchool(List<String> userIds, String roleEnName, String schoolId)throws TException {
	    String result = null;
	    if (StringUtils.isBlank(roleEnName)) {
	      roleEnName = "teacher";
	    }
	    CyClientFactory cyClientFactory = new CyClientFactory();
	      result = cyClientFactory.createUserSvc().set_user_to_school(userIds, roleEnName, schoolId);
	    return Boolean.valueOf("1".equals(result));
	  }
	
	
	public static Boolean removeUserFromSchoolToHistory(List<String> userIds, String roleEnName, String schoolId, boolean toHistory)throws TException  {
		String result = null;
		if (StringUtils.isBlank(roleEnName)) {
		  roleEnName = "teacher";
		}
		CyClientFactory cyClientFactory = new CyClientFactory();
		result = cyClientFactory.createUserSvc().remove_user_from_school_to_history(userIds, roleEnName, schoolId, toHistory);
		return Boolean.valueOf("1".equals(result));
	}
	
	public static List<TRole> listRoleByUser(String userId) throws TException {
		CyClientFactory cyClientFactory = new CyClientFactory();
    	List<TRole> roles = cyClientFactory.createRoleSvc().list_role_by_user(userId);
    	return roles;
	}
	
	 public static boolean removeUserManageSchool(String userId, List<String> areaIds) throws TException {
		 CyClientFactory cyClientFactory = new CyClientFactory();
		 String result = cyClientFactory.createUserSvc().remove_user_manage_school(userId, areaIds);
		 return "1".equals(result);
	 }
	
	 public static boolean removeUserManageClass(String userId, List<String> classIds)  throws TException {
//	    CyClientFactory cyClientFactory = new CyClientFactory();
//	    cyClientFactory.createUserSvc().remove_user_manage_eduorg(userId, classIds);
//	    cyClientFactory.createUserSvc().remove_user_from_class(arg0, arg1, arg2)
//		 String result = cyClientFactory.createUserSvc().remove_user_manage_class(userId, classIds);
//	    return "1".equals(result);
		 return true;
	  }
	
	
	
	
	public static List<UserManager> listUserManagerByUserId(String userId) throws TException {
	    List<TUserManager> tUserManagerList = new ArrayList();
	    List<UserManager> managerResult = new ArrayList();
	    CyClientFactory cyClientFactory = new CyClientFactory();
		List<TUserManager> f = cyClientFactory.createUserSvc().list_user_manager_scope_by_user(userId);
	    managerResult = UserManager.toUserManagerList(f);
	    return managerResult;
	}
	
	
	
	
	
	// 修改密码
	public static void reset_password_by_unique_info() throws Exception {
		CyClientFactory cyClientFactory = new CyClientFactory();
		String result = cyClientFactory.createUserSvc().reset_password_by_unique_info("login_name", TObject.strVal("bl_ptls"), "123456");
		System.out.println(result);
	}
	
	// 查询教师用户
	public static void retrieveTeacherExt() throws Exception {
		CyClientFactory cyClientFactory = new CyClientFactory();
		Map<String, TObject> arg0 = new HashMap<String, TObject>();
		arg0.put("skip", TObject.intVal(0));
		arg0.put("limit", TObject.intVal(100));
		arg0.put("with_all_children", TObject.boolVal(true));
		arg0.put("app_name", TObject.strVal("rrt_zhangye"));
		arg0.put("order", TObject.strVal("-id"));
		List<TUser> result = cyClientFactory.createUserSvc().retrieve_teacher_ext(arg0);
		System.out.println(result.size());
		for (TUser t:result) {
			System.out.println(t.getLoginName());
		}
		
	}
	
	
	
	
}
